package g4meOne;

import java.util.ArrayList;
import java.util.Random;

public class Calculs {
	private ArrayList<Integer[]> calculation;
	private ArrayList<String> operator;
	private boolean addition;
	private boolean subtraction;
	private boolean relatif;
	private boolean multiplication;
	private boolean division;
	private Random random;
	private boolean table=false;
	private int[] tableToReview;
	int numberOperator=0;


	public Calculs(){
		this(true,true,false,true,false);
	}

	public Calculs(int...x){
		calculation=new ArrayList<Integer[]>();
		operator=new ArrayList<String>();
		random=new Random();
		table=true;
		tableToReview=x;
	}

	public Calculs(boolean add, boolean sub,boolean rel, boolean mul, boolean div){
		calculation=new ArrayList<Integer[]>();
		operator=new ArrayList<String>();
		random=new Random();
		if(add || sub || mul || div){
			this.addition=add;
			this.subtraction=sub;
			this.multiplication=mul;
			this.division=div;
			this.relatif=rel;
		}else{
			this.addition=true;
		}

		if(addition) numberOperator++;
		if(subtraction) numberOperator++;
		if(multiplication) numberOperator++;
		if(division) numberOperator++;
	}

	public void generateOneCal(){
		if(!table){
			int i=random.nextInt(numberOperator);
			if (numberOperator==1){
				if(addition){
					generateSum();
				}else if(subtraction){
					generateSub();
				}else if(multiplication){
					generateMul();
				}else{
					generateDiv();
				}
			}else if(numberOperator==2){
				if (addition && subtraction){
					if (i==0) generateSum();
					if (i==1) generateSub();
				}else if(addition && multiplication){
					if (i==0) generateSum();
					if (i==1) generateMul();
				}else if (addition && division){
					if (i==0) generateSum();
					if (i==1) generateDiv();
				}else if(subtraction && multiplication){
					if (i==0) generateSub();
					if (i==1) generateMul();
				}else if(subtraction && division){
					if (i==0) generateSub();
					if (i==1) generateDiv();
				}else if(multiplication && division){
					if (i==0) generateDiv();
					if (i==1) generateMul();
				}
			}else if(numberOperator==3){
				if (addition && subtraction && multiplication){
					if (i==0) generateSum();
					if (i==1) generateSub();
					if (i==2) generateMul();
				}else if(addition && subtraction && division){
					if (i==0) generateSum();
					if (i==1) generateSub();
					if (i==2) generateDiv();
				}else if(addition && multiplication && division){
					if (i==0) generateSum();
					if (i==1) generateMul();
					if (i==2) generateDiv();
				}else if(subtraction && multiplication && division){
					if (i==0) generateSub();
					if (i==1) generateMul();
					if (i==2) generateDiv();
				}
			}else{
				if (i==0) generateSum();
				if (i==1) generateSub();
				if (i==2) generateMul();
				if (i==3) generateDiv();
			}
		}
		else{
			generateTableToReview();
		}
	}
	
	private void generateTableToReview(){
		Integer[] mul={tableToReview[random.nextInt(tableToReview.length)],random.nextInt(12)+1};
		operator.add("x");
		calculation.add(mul);
	}

	private void generateSum(){
		Integer[] somme={random.nextInt(10)+1,random.nextInt(10)+1};
		operator.add("+");
		calculation.add(somme);
	}

	private void generateSub(){
		if(relatif){
			Integer[] sub={random.nextInt(10)+1,random.nextInt(10)+1};
			operator.add("-");
			calculation.add(sub);
		}else{
			int n = random.nextInt(10)+1;
			int i = random.nextInt(n)+1;
			Integer[] sub={n,i};
			operator.add("-");
			calculation.add(sub);
		}
	}

	private void generateMul(){
		Integer[] mul={random.nextInt(10)+1,random.nextInt(10)+1};
		operator.add("x");
		calculation.add(mul);
	}

	private void generateDiv(){
		Integer n=random.nextInt(10)+1;
		Integer[] div={n*(random.nextInt(10)+1),n};
		operator.add("/");
		calculation.add(div);
	}

	public void clear(){
		calculation.clear();
		operator.clear();
	}

	public String getOperator(int i){
		return operator.get(i);
	}

	public Integer[] getCalcul(int i){
		return calculation.get(i);
	}

	public int size(){
		return calculation.size();
	}

	public int getResult(int i){
		int n=0;
		if (operator.get(i).equals("+")) n= calculation.get(i)[0]+calculation.get(i)[1];
		if (operator.get(i).equals("-")) n= calculation.get(i)[0]-calculation.get(i)[1];
		if (operator.get(i).equals("x")) n= calculation.get(i)[0]*calculation.get(i)[1];
		if (operator.get(i).equals("/")) n= calculation.get(i)[0]/calculation.get(i)[1];
		return n;
	}

	public void remove(int i){
		calculation.remove(i);
		operator.remove(i);
	}

	public boolean contains(int resultat){
		boolean contain=false;
		for(int i=0; i < calculation.size(); i++){
			if(getResult(i)==resultat) contain=true;
		}
		return contain;
	}
}
